#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // listWidget,QLIstWidgetItem这个类也可以操作它的字体属性、图标等
    ui->listWidget->addItem(new QListWidgetItem("1234"));
    ui->listWidget->addItem("4321");
    ui->listWidget->insertItem(1,new QListWidgetItem("666"));

    // tableWidget
    ui->tableWidget->insertRow(0);
    ui->tableWidget->insertRow(1);
    ui->tableWidget->insertRow(2);
    ui->tableWidget->insertColumn(0);
    ui->tableWidget->insertColumn(1);
    ui->tableWidget->insertColumn(2);

    ui->tableWidget->setHorizontalHeaderItem(0,new QTableWidgetItem("学号"));
    ui->tableWidget->setHorizontalHeaderItem(1,new QTableWidgetItem("姓名"));
    ui->tableWidget->setHorizontalHeaderItem(2,new QTableWidgetItem("年龄"));

    ui->tableWidget->setVerticalHeaderItem(0,new QTableWidgetItem("1"));
    ui->tableWidget->setVerticalHeaderItem(1,new QTableWidgetItem("2"));
    ui->tableWidget->setVerticalHeaderItem(2,new QTableWidgetItem("3"));

    ui->tableWidget->setItem(0,0,new QTableWidgetItem("1001"));
    ui->tableWidget->setItem(0,1,new QTableWidgetItem("张三"));
    ui->tableWidget->setItem(0,2,new QTableWidgetItem("18"));

    ui->lineEdit_table->setPlaceholderText("请输入新增的列名：");

    //treeWidget
    ui->treeWidget->setHeaderLabel("动物");
    QTreeWidgetItem* item = new QTreeWidgetItem();
    item->setText(0, "猫");
    ui->treeWidget->addTopLevelItem(item);

    ui->lineEdit_tree->setPlaceholderText("请输入插入元素名称：");

    // groupBox分组
    QGroupBox* groupBox = new QGroupBox(this);
    groupBox->setTitle("test");
    groupBox->setGeometry(360, 411, 421, 111);
    QComboBox* comboBox = new QComboBox(groupBox);
    comboBox->setGeometry(20, 30, 201, 41);
}

Widget::~Widget()
{
    delete ui;
}

// 增加删除listWidget元素
void Widget::on_pushButton_add_clicked()
{
    const QString& text = ui->lineEdit_list->text();
    ui->listWidget->addItem(text);
}

void Widget::on_pushButton_sub_clicked()
{
    int row = ui->listWidget->currentRow();
    if (row < 0)
        return;
    ui->listWidget->takeItem(row);
}
// 元素状态改变时触发
void Widget::on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    if (current)
        qDebug() << "当前选中的元素为：" << current->text();
    if (previous)
        qDebug() << "上次选中的元素为：" << previous->text();
}
// tableWidget
void Widget::on_pushButton_insertrow_clicked()
{
    int row = ui->tableWidget->rowCount();
    ui->tableWidget->insertRow(row);
}

void Widget::on_pushButton_insertcolumn_clicked()
{
    int column = ui->tableWidget->columnCount();
    ui->tableWidget->insertColumn(column);

    QString text = ui->lineEdit_table->text();
    ui->tableWidget->setHorizontalHeaderItem(column, new QTableWidgetItem(text));
}

void Widget::on_pushButton_deleterow_clicked()
{
    int row = ui->tableWidget->currentRow();
    ui->tableWidget->removeRow(row);
}

void Widget::on_pushButton_deletecolumn_clicked()
{
    int column = ui->tableWidget->currentColumn();
    ui->tableWidget->removeColumn(column);
}
// treeWidget
void Widget::on_pushButton_inserttop_clicked()
{
    const QString& text = ui->lineEdit_tree->text();

    QTreeWidgetItem* item = new QTreeWidgetItem();
    item->setText(0, text);
    ui->treeWidget->addTopLevelItem(item);
}

void Widget::on_pushButton_insertcur_clicked()
{
    const QString& text = ui->lineEdit_tree->text();
    QTreeWidgetItem* itemcur = ui->treeWidget->currentItem();
    if(itemcur == nullptr)
        return;

    QTreeWidgetItem* item = new QTreeWidgetItem();
    item->setText(0, text);
    itemcur->addChild(item);
}

void Widget::on_pushButton_deletecur_clicked()
{
    QTreeWidgetItem* itemcur = ui->treeWidget->currentItem();
    if(itemcur == nullptr)
        return;
    // 删除元素需要通过父元素进行删除
    QTreeWidgetItem* parent = itemcur->parent();
    if(parent == nullptr)
    {
        // 顶层元素
        int index = ui->treeWidget->indexOfTopLevelItem(itemcur);
        ui->treeWidget->takeTopLevelItem(index);
    }
    else
    {
        //普通元素
        parent->removeChild(itemcur);
    }
}
